<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /*
     1. 数组求和及平均值
     let arr1 = [7, 2, 1, 17, 23];
    */
      let arr1 = [7, 2, 1, 17, 23];
      let sum1 = [];
      sum1 = arr1.reduce(function (total, current) {
        total += current;
        return total;
      }, 0);
      console.log('sum1', sum1);

      let sum11 = [];
      sum11 = arr1.reduce(function (total, current) {
        total += current;
        return total;
      }, 0);
      let a = 0;
      a = sum11 / 5;
      console.log('a', a);

      /*
    2. 求数组[12,52,12,7,123,5,17]中的最大值。
    */
      let arr2 = [12, 52, 12, 7, 123, 5, 17];
      arr2.sort(function (a, b) {
        return b - a;
      });
      console.log(arr2.slice(0, 1));
      /*
    3. 将数组 ['red', 'green', 'blue', 'pink'] 里面的元素转换为字符串, 输出“redgreenbluepink”
    */
      let arr3 = ['red', 'green', 'blue', 'pink'];
      console.log(arr3.join(''));

      /*
    4. 将数组 ['red', 'green', 'blue', 'pink'] 转换为字符串，并且用 | 或其他符号分割, 输出: 'red|green|blue|pink'
    */
      let arr4 = ['red', 'green', 'blue', 'pink'];
      console.log(arr4.join('|'));

      /*
      5. 把数组[2,5,6,7,8]中的每一位数字都增加30%，并返回一个新数组
      */
      let arr5 = [2, 5, 6, 7, 8];
      let sum5 = [];
      sum5 = arr5.map(function (value) {
        value += value * 0.3;
        return value;
      });
      console.log('sum5', sum5);
      /*
      6. 编写函数has(arr, 20) 判断数组中是否存在20这个元素，返回布尔类型
      */
      let arr6 = [1, 21, 21, 21, 29, 22];
      let n2 = 20;
      function has(arr6, n2) {
        let sum6 = true;
        sum6 = arr6.some(function (value) {
          return value === 20;
        });
        console.log(sum6);
      }
      has(arr6, n2);
      /*
      7. 根据数组中对比age的年龄, 从大到小排序
      let arr7 = [
    {age: 19, name:'a'},
    {age: 50, name:'b'},
    {age: 12, name:'c'},
    {age: 25, name:'d'}
      ]
      */
      let arr7 = [
        { age: 19, name: 'a' },
        { age: 50, name: 'b' },
        { age: 12, name: 'c' },
        { age: 25, name: 'd' },
      ];
      arr7.sort(function (a, b) {
        return b.age - a.age;
      });
      console.log('arr7', arr7);
      /*
      8. 改变传入的数组，将数组中第 n(从 0 开始算 ) 个元素放到数组的开头
      */
      function move(arr8, n) {
        let sum8 = arr8[n];
        arr8.splice(n, 1);
        arr8.unshift(sum8);
        return arr8;
      }
      let arr8 = [1, 2, 3, 4, 5];
      let n = 3;
      console.log(move(arr8, n));

      /*
      9. 将 函数中的arguments 对象转换成真正的数组
      */
      function my() {
        const arr9 = Array.from(arguments);
        console.log(arr9);
      }
      my(1, 2, 3, 4);

      /*
      10. 如何实现一个数组push方法
      */
      let arr10 = [1, 2, 3]; // 创建一个数组
      arr10.push(4); // 在数组的末尾添加一个元素4
      console.log(arr10); //[1,2,3,4]
      arr10.push(5, 6, 7); //可添加多个
      console.log('arr10', arr10); //[1,2,3,4,5,6,7]
      /*
      11. 数组去重的方法 (4种)
      */
      let arr11 = [1, 2, 3, 2, 1, 4, 5, 4];
      let sum111 = Array.from(new Set(arr11));
      console.log(sum111);

      let array = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4];
      let uniqueArray = [];
      for (let i = 0; i < array.length; i++) {
        if (!uniqueArray.includes(array[i])) {
          uniqueArray.push(array[i]);
        }
      }
      console.log(uniqueArray);
    </script>
  </body>
</html>
